$(function () {
    $("[name=province]").change(function () {
        var id=$(this).val();
        console.log("省的id"+id);
        $("[name=area]").html("<option>下拉选择</option>");
         $.ajax({
             url:"/area",
             data:{
                method:"getCityArea",
                pid:id,
             },
             type: "get",
             dataType: "json",
             beforeSend: function (x) {
                 x.setRequestHeader("xhr", "ajax")
             },
             success:(function (data) {
                var val="<option>下拉选择</option>";
                for ( var i in data){
                    console.log(data[i]);
                    val+="<option value='"+data[i].id+"'>"+data[i].name+"</option>";
                }
                 $("[name=city]").html(val);
             }),
         })
        console.log("获取市的ajax发完了");
    })

    $("[name=city]").change(function () {
        var id=$(this).val();
        console.log("市的id"+id);
        $.ajax({
            url:"/area",
            data:{
                method:"getCityArea",
                cid:id,
            },
            type: "get",
            dataType: "json",
            beforeSend: function (x) {
                x.setRequestHeader("xhr", "ajax")
            },
            success:(function (data) {
                var val="<option>下拉选择</option>";
                for ( var i in data){
                    console.log(data[i]);
                    val+="<option value='"+data[i].name+"'>"+data[i].name+"</option>";
                }
                $("[name=area]").html(val);
            }),
        })
        console.log("获取区d的ajax发完了");
    })


    $("#addressfrom").on("submit",function () {
        // alert($("#addressfrom").serialize());
        $.ajax({
            url:"addr?"+$("#addressfrom").serialize(),
            data:{
                method:"add",
            },
            type:"get",
            dataType:"json",
            success:function (data) {
                console.log(data);
                var val=$("#address").html();
                console.log(val);
                if (val=="<option>尚未创建收货地址</option>") {
                    val = "<option>" + data.content + "-" + data.uname + "-" + data.utel + "</option>";
                    $("#address").html(val);
                }else{
                    val+= "<option>" + data.content + "-" + data.uname + "-" + data.utel + "</option>";
                }                //模态框消失
                if (data!=null){

                    alert("添加成功！");//继续完善,用resultmsg
                }
            }
        })
        return false;
    })

    // http://localhost:8080/order?province=24&city=311&area=莲湖区&detail=科技路&uname=宋文君&utel=132&setdefault=
    $("#address").change(function () {
        console.log($(this).val());
        var aid=($(this).val());
        $.ajax({
            url:"addr",
            data:{
                method:"setDefault",
                aid:aid,
                },
            type:"get",
            dataType:"json",
            success:function (data) {
                if(data.code==1){
                    alert("默认收货地址已修改");
                }
            }
        })
    })




    $('#myModal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget) // Button that triggered the modal
        var recipient = button.data('whatever') // Extract info from data-* attributes
        // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
        // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
        var modal = $(this);
        modal.find('.modal-title').text('创建新收获地址');//+ recipient
        modal.find('.modal-body input').val(recipient);
    })

})
